package com.yzf;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.yao.component.circle.R;
import com.yao.sdk.cache.filestore.FileStoreProxy;
import com.yao.sdk.util.Utils;

/**
 * AMapV1中简单介绍混合定位
 */
public class LocationNetworkActivity extends Activity implements AMapLocationListener {
	private TextView myLocation;
	private AMapLocation aMapLocation;// 用于判断定位超时
	private Handler handler = new Handler();
	private Double geoLat;
	private Double geoLng;
	private String cityName = "";
	private AMapLocationClient locationClient;
	private AMapLocationClientOption locationOption;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locationnetwork_activity);
		myLocation = (TextView) findViewById(R.id.myLocation);
//		aMapLocManager = LocationManagerProxy.getInstance(this);
//		/*
//		 * mAMapLocManager.setGpsEnable(false);//
//		 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
//		 * API定位采用GPS和网络混合定位方式
//		 * ，第一个参数是定位provider，第二个参数时间最短是2000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
//		 */
//		aMapLocManager.requestLocationData(
//				LocationProviderProxy.AMapNetwork, 2000, 10, this);
//		handler.postDelayed(this, 12000);// 设置超过12秒还没有定位到就停止定位
		Location();
		findViewById(R.id.search_arround).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LocationNetworkActivity.this, ArroundAddressListActivity.class);
				intent.putExtra("cityName",cityName);
				intent.putExtra("Lat",geoLat);
				intent.putExtra("Lon",geoLng);
				startActivity(intent);
			}
		});
	}

	private void Location() {
		locationClient = new AMapLocationClient(getApplicationContext());
		locationOption = new AMapLocationClientOption();
		//设置为单次定位
		locationOption.setOnceLocation(true);
		// 设置定位模式为低功耗模式
		locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
		// 设置定位监听
		locationClient.setLocationListener(this);
		// 设置是否需要显示地址信息
		locationOption.setNeedAddress(true);
		// 设置定位参数
		locationClient.setLocationOption(locationOption);
		// 启动定位
		locationClient.startLocation();
		mHandler.sendEmptyMessage(Util.MSG_LOCATION_START);
	}

	Handler mHandler = new Handler(){
		public void dispatchMessage(android.os.Message msg) {
			switch (msg.what) {
				case Util.MSG_LOCATION_START:
//                    top_left_tv.setText("正在定位...");
					break;
				//定位完成
				case Util.MSG_LOCATION_FINISH:
					AMapLocation loc = (AMapLocation)msg.obj;
					double latitude = loc.getLatitude();
					double longitude = loc.getLongitude();
					String city = loc.getCity();
					if (!Utils.isEmpty(city)){
						city = city.substring(0, city.length() - 1);
						FileStoreProxy.setValue("cityname",city);
					}
					FileStoreProxy.setValue("latitude",latitude+"");//纬度
					FileStoreProxy.setValue("longitude",longitude+"");//经度
					String result = Util.getLocationStr(loc);
					cityName = loc.getCity();
					myLocation.setText(loc.getAddress());
					break;
				default:
					break;
			}
		};
	};

	// 定位监听
	@Override
	public void onLocationChanged(AMapLocation loc) {
		if (null != loc) {
			Message msg = mHandler.obtainMessage();
			msg.obj = loc;
			msg.what = Util.MSG_LOCATION_FINISH;
			mHandler.sendMessage(msg);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (null != locationClient) {
			/**
			 * 如果AMapLocationClient是在当前Activity实例化的，
			 * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
			 */
			locationClient.onDestroy();
			locationClient = null;
			locationOption = null;
		}
	}

}
